let startPoint = null;
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    buttonTop: 0,
    buttonLeft: 0,
    windowHeight: 0,
    windowWidth: 0,
  },

  /**
   * 组件的方法列表
   */
  pageLifetimes: {
    show() {
      wx.getSystemInfo({
        success: res => {
          let h = res.windowHeight,
            w = res.windowWidth;
          this.setData({
            windowHeight: h,
            windowWidth: w,
            buttonLeft: w * 0.795,
            buttonTop: h * 0.66,
          });
        },
      });
    },
  },
  methods: {
    buttonStart(e) {
      startPoint = e.touches[0];
    },
    buttonMove(e) {
      //计算拖动后的坐标
      let endPoint = e.touches[e.touches.length - 1],
        disX = endPoint.clientX - startPoint.clientX,
        disY = endPoint.clientY - startPoint.clientY;
      startPoint = endPoint;
      let buttonTop = this.data.buttonTop + disY,
        buttonLeft = this.data.buttonLeft + disX;
      //判断是否超出屏幕
      if (buttonLeft + 52 >= this.data.windowWidth) {
        buttonLeft = this.data.windowWidth - 52;
      }
      if (buttonLeft <= 0) {
        buttonLeft = 0;
      }
      if (buttonTop + 52 >= this.data.windowHeight) {
        buttonTop = this.data.windowHeight - 52;
      }
      if (buttonTop <= 0) {
        buttonTop = 0;
      }

      this.setData({
        buttonTop,
        buttonLeft,
      });
    },
  },
});
